package cc.zy.base.hr.service.impl;


import cc.zy.base.common.entity.FebsConstant;
import cc.zy.base.common.entity.QueryRequest;
import cc.zy.base.common.utils.SortUtil;
import cc.zy.base.hr.entity.ExamineSimple;
import cc.zy.base.hr.mapper.ExamineSimpleMapper;
import cc.zy.base.hr.service.IExamineSimpleService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @author TK
 * @time 2022/03/09 10:10:13
 *
 * 考卷service实现类
 *
 */

@Service
@RequiredArgsConstructor
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
public class ExamineSimpleServiceImpl extends ServiceImpl<ExamineSimpleMapper, ExamineSimple> implements IExamineSimpleService {

    @Resource
    private final ExamineSimpleMapper examineSimpleMapper;

    /**
     * @Description 查询所有，带分页
     * @param request:
     * @param examineSimple:
     * @return com.baomidou.mybatisplus.core.metadata.IPage<cc.zy.base.hr.entity.ExamineSimple>
     * @Author TK
     */
    @Override
    public IPage<ExamineSimple> findExamineSimples(QueryRequest request, ExamineSimple examineSimple) {

        Page<ExamineSimple> page = new Page<>(request.getPageNum(), request.getPageSize());
        page.setSearchCount(false);
        page.setTotal(baseMapper.countExamineSimpleDetail(examineSimple));
        SortUtil.handlePageSort(request, page, "id", FebsConstant.ORDER_ASC, false);

        return baseMapper.listExamineSimpleDetailPage(page, examineSimple);
    }

    /**
     * @Description 通过id查询对象
     * @param id:
     * @return cc.zy.base.hr.entity.ExamineSimple
     * @Author TK
     */
    @Override
    public ExamineSimple findById(Integer id) {
        return baseMapper.getById(id);
    }

    /**
     * @Description 删除用户组
     * @param examinationIds: 传入的多个id
     * @return void
     * @Author TK
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteExamineSimples(String[] examinationIds) {

        List<String> list = Arrays.asList(examinationIds);
        // 删除用户
        this.removeByIds(list);
    }

    /**
     * @Description 查找考卷，不分页
     * @param examineSimple: 删除对象
     * @return java.util.List<cc.zy.base.hr.entity.ExamineSimple>
     * @Author TK
     */
    @Override
    public List<ExamineSimple> findExamineSimpleListNotPage(ExamineSimple examineSimple) {

        List<ExamineSimple> examineSimpleListNotPage = baseMapper.listExamineSimpleListNotPage(examineSimple);

        return examineSimpleListNotPage;
    }

    /**
     * @Description 通过id查询考卷
     * @param id:
     * @return cc.zy.base.hr.entity.ExamineSimple
     * @Author TK
     */
    @Override
    public ExamineSimple findExamineSimpleDetailList(Integer id) {

        ExamineSimple param = new ExamineSimple();
        param.setId(id);
        List<ExamineSimple> examineSimple = baseMapper.listExamineSimpleDetail(param);

        return CollectionUtils.isNotEmpty(examineSimple) ? examineSimple.get(0) : null;
    }

    /**
     * @Description
     * @param examineSimple:
     * @return java.util.List<cc.zy.base.hr.entity.ExamineSimple>
     * @Author TK
     */
    @Override
    public List<ExamineSimple> findExamineSimples(ExamineSimple examineSimple) {

        LambdaQueryWrapper<ExamineSimple> queryWrapper = new LambdaQueryWrapper<>();
        return baseMapper.selectList(queryWrapper);
    }

    /**
     * @Description 添加考卷
     * @param examineSimple: 添加对象
     * @return void
     * @Author TK
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void createExamineSimple(ExamineSimple examineSimple) {

        examineSimple.setCreateDate(new Date());
        examineSimple.setLastModifyDate(new Date());

        this.save(examineSimple);
    }

    /**
     * @Description 修改考卷
     * @param examineSimple: 修改对象
     * @return void
     * @Author TK
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateExamineSimple(ExamineSimple examineSimple) {
        this.saveOrUpdate(examineSimple);
    }

    /**
     * @Description 删除考卷
     * @param examineSimple:
     * @return void
     * @Author TK
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteExamineSimple(ExamineSimple examineSimple) {

        LambdaQueryWrapper<ExamineSimple> wrapper = new LambdaQueryWrapper<>();

        this.remove(wrapper);
    }

    /**
     * @Description 查询前台输入的考卷名称是否存在
     * @param title: 传入的考卷名称
     * @return java.lang.Boolean
     * @Author TK
     */
    @Override
    public Boolean isRepetitive(String title) {

        List<ExamineSimple> repetitive = examineSimpleMapper.isRepetitive(title);
        Boolean flag = true;
        if (repetitive != null && repetitive.size() > 0) {
            flag = false;
        }
        return flag;
    }

}